import React,{Component} from 'react';
import {Form, Input} from 'antd'
import { PropTypes } from 'prop-types'
import { reqDictfindById } from "../../../api/dict";


class AddOrUpdate extends Component{
    static propTypes = {
        setForm: PropTypes.func.isRequired,
        dataId: PropTypes.string,
    }
    state = {
        updateValues: {}
    }
    componentWillMount() {
        console.log('aaaaa',this.props.dataId)
        this.props.setForm(this.props.form)
        if(this.props.dataId){
            this.getUpdateValue()
        }
    }
    getUpdateValue = () => {
        if (this.props.dataId) {
            reqDictfindById({id:this.props.dataId}).then(({data}) => {
                this.setState({updateValues: Object.assign(this.state.updateValues, data.list)})
            })
        }
    }

    render(){
        // setTimeout(()=>{
        //     this.getUpdateValue()
        // },0)
        const form = this.props.form
        const { getFieldDecorator } = form;
        const {type, typeName, dicKey, dicValue, dicSort} = this.state.updateValues
        return(
            <Form labelCol={{ span: 3 }} wrapperCol={{ span: 21 }}>
                <Form.Item label="类型">
                    {getFieldDecorator('type', {
                        initialValue:type,
                        rules: [
                            {
                                required: true,
                                message: '请输入类型',
                            },
                        ],
                    })(<Input />)}
                </Form.Item>
                <Form.Item label="类型名称">
                    {getFieldDecorator('typeName', {
                        initialValue:typeName,
                        rules: [
                            {
                                required: true,
                                message: '请输入类型名称',
                            },
                        ],
                    })(<Input />)}
                </Form.Item>
                <Form.Item label="键值">
                    {getFieldDecorator('dicKey', {
                        initialValue:dicKey,
                        rules: [
                            {
                                required: true,
                                message: '请输入键值',
                            },
                        ],
                    })(<Input />)}
                </Form.Item>
                <Form.Item label="内容">
                    {getFieldDecorator('dicValue', {
                        initialValue:dicValue,
                        rules: [
                            {
                                required: true,
                                message: '请输入内容',
                            },
                        ],
                    })(<Input />)}
                </Form.Item>
                <Form.Item label="排序">
                    {getFieldDecorator('dicSort', {
                        initialValue:dicSort,
                        rules: [
                            {
                                required: true,
                                message: '请输入排序',
                            },
                        ],
                    })(<Input />)}
                </Form.Item>
            </Form>
        )
    }
}

const WrapLogon = Form.create()(AddOrUpdate)
export default WrapLogon